package com.shopping.service.order;

import com.alibaba.fastjson.JSONObject;
import com.shopping.constant.ResponseCode;
import com.shopping.dao.OrderDao;
import com.shopping.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDao orderDao;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    JSONObject json = new JSONObject();

    @Override
    public Map<String, Object> addOrder(Order order) {
        Map<String, Object> map = new HashMap<>();
        try {
            orderDao.insert(order);
            int id = orderDao.lastInsertId();
            order.setId(id);
            List<Order> orderList = json.parseArray(stringRedisTemplate.opsForValue().get("order" + order.getUserId()), Order.class);
            orderList.add(order);
            stringRedisTemplate.opsForValue().set("order" + order.getUserId(), json.toJSONString(orderList));
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("order", order);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> showUsersOrders(int userId) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Order> orders = null;
            orders = json.parseArray(stringRedisTemplate.opsForValue().get("order" + userId), Order.class);
//            orders = orderDao.showUserOrders(userId);
            if(orders.size() == 0){
                map.put("code", ResponseCode.ENTITY_NOT_EXISTS.getValue());
                return map;
            }
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("orders", orders);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> showStoreOrders(int storeId) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Order> orders = orderDao.showStoreOrders(storeId);
            if(orders.size() == 0){
                map.put("code", ResponseCode.ENTITY_NOT_EXISTS.getValue());
                return map;
            }
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("orders", orders);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> showOneOrder(int id) {
        Map<String, Object> map = new HashMap<>();
        try {
            Order order = orderDao.showOneOrder(id);
            if(order == null){
                map.put("code", ResponseCode.ENTITY_NOT_EXISTS.getValue());
                return map;
            }
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("order", order);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> isOrderExist(int userId, int storeId, int tradeId) {
        Map<String, Object> map = new HashMap<>();
        try{
            Order order = null;
            List<Order> orderList = json.parseArray(stringRedisTemplate.opsForValue().get("order" + userId), Order.class);
            for(Order o : orderList){
                if(o.getStoreId() == storeId && o.getTradeId() == tradeId){
                    map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
                    order = o;
                    break;
                }
            }
            if (order == null){
                order = orderDao.selectOrderExist(userId, storeId, tradeId);
                if(order == null){
                    map.put("code", ResponseCode.ENTITY_NOT_EXISTS.getValue());
                    return map;
                }
                map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            }
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> selectUserIdByOrderId(int orderId) {
        Map<String, Object> map = new HashMap<>();
        try{
            int userId = orderDao.selectUserIdByOrderId(orderId);
            map.put("userId", userId);
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }
}
